package code.temp.log.bedpicMove;

import com.github.cosycode.common.util.common.StrUtils;
import com.github.cosycode.common.util.io.FileSystemUtils;
import com.github.cosycode.common.util.io.IoUtils;
import com.github.cosycode.common.util.reflex.ReflexUtils;
import com.github.cosycode.ext.se.util.FileUtils;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

/**
 * <b>Description : </b>
 * <p> 1. 确定根目录, 搜索并过滤指定目录下文件</p>
 * <b>created in </b> 2021/4/30
 *
 * @author CPF
 * @since
 **/
public class PicMove {

    public static final String MARKDOWN_IMG_REGEX = "!\\[[^\\[\\]]*?\\]\\(([^\\(\\)]+?)\\)";
    public static final String MARKDOWN_IMG_REGEX_DIRECTOR = "(?<=!\\[[^\\[\\]]{1,100}?\\]\\()[^\\(\\)]+?(?=\\))";

    enum PathType {
        NET, RELATION_FILE, RELATION_ROOT, ABSOLUTE
    }

    public static void move(String rootDir, File[] searchDir, FileFilter fileFilter) throws IOException {
        // 1. 遍历文件夹, 找出符合条件的文件
        Set<String> filePath = new HashSet<>();
        List<File> files = new ArrayList<>();
        for (File dir : searchDir) {
            FileSystemUtils.fileDisposeByLoop(dir, file->{
                final String path = file.getPath();
                if (!filePath.contains(path)) {
                    filePath.add(path);
                    files.add(file);
                }
            }, fileFilter);
        }
        // 2.
        for (File file : files) {
            final String s = FileUtils.analysisTextFileEncode(file);
            final Charset charset = Charset.forName(s);
            final FileInputStream inputStream = new FileInputStream(file);
            final String s1 = IoUtils.readStringFromInputStream(inputStream, charset);
            StrUtils.replaceAll(MARKDOWN_IMG_REGEX_DIRECTOR, s1, path -> {
                String computePath = computePath(rootDir, file, path);
                if (path.startsWith("http")) {
                    boolean download = HttpUtils.download(path, new File(computePath), null);
                }
                return computePath;
            });
//            StrUtils.replaceAll("(?<=!\\[[^\\[\\]]*?\\]\\()([^\\(\\)]+?)(?=\\))", s1, str -> {
//            })
            IoUtils.writeStringToOutputStream(new FileOutputStream(file), s1, charset);
        }
    }

    /**
     * @param rootDir 项目根路径
     * @param file
     * @param path 路径:{根路径, 相对路径, 绝对路径}
     * @return 绝对路径
     */
    public static String computePath(String rootDir, File file, String path) {
        if (path.startsWith("/")) {
            return rootDir + path;
        } else if (path.startsWith("http")) {
            return rootDir + "/" + path;
        } else if (path.startsWith(".")) {
            return file.getParentFile() + File.separator + path;
        } else {
            return path;
        }
    }


    public static void main(String[] args) throws IOException, URISyntaxException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        final File file = new File("D:/Users\\.//CPF\\Desktop\\out\\fere/../realtime/main-screen-shot-20210413-124144-266.png");

        System.out.println(file.getPath());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.exists());
        System.out.println(file.getCanonicalPath());
        System.out.println(file.getParentFile());
        System.out.println();

        final Object fs = ReflexUtils.getAttributeFromObject(new File(""), "fs");
        final Method canonicalize = fs.getClass().getDeclaredMethod("canonicalize", String.class);
        canonicalize.setAccessible(true);
        final Object invoke = canonicalize.invoke(fs, "D:\\Users\\.\\CPF\\Desktop\\out\\fere\\..\\realtime");
        System.out.println(invoke);
    }

}
